package com.mashuai.utils.base;

import com.mashuai.utils.toolkit.core.msConverts.MsConvertDataUtils;
import com.mashuai.utils.toolkit.inter.functionalInterface.base.FMsSelfBehavior;

import java.util.Collection;
import java.util.Map;

/**
 * @ClassName: MsEmptyUtils
 * @Description: 为空判断
 */
public class MsEmptyUtils {

    /**
     * 字符串不为空判断
     *
     * @param cs
     * @return
     * @author mashuai
     */

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isNotEmpty(Object cs) {
        return !isEmpty(cs);
    }

    /***
     * 如果字符串不是空，执行后面操作
     * @param cs
     * @param fuc
     * @return:
     * @author:mashuai
     */
    public static void isNotEmpty(Object cs, FMsSelfBehavior fuc) {
        if (!MsConvertDataUtils.checkObjValIsEmpty(cs)) fuc.behavior();
    }


    /**
     * 字符串为空判断
     *
     * @param cs
     * @return
     * @author mashuai
     */

    public static boolean isEmpty(CharSequence cs) {
        int strLen = length(cs);
        if (strLen != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isEmpty(Object cs) {
        if (cs == null) return true;
        String csStr = cs.toString();
        return isEmpty(csStr);
    }

    public static void isEmpty(Object cs, FMsSelfBehavior fuc) {
        if (MsConvertDataUtils.checkObjValIsEmpty(cs)) fuc.behavior();
    }

    /**
     * 如果值为空就设置默认值
     *
     * @param val
     * @param defVal
     * @return
     * @author mashuai
     */

    public static <T> T ifNullSetDefVal(T val, T defVal) {
        //传入对象为null抛异常
        if (null == val) return defVal;
        String strVal = String.valueOf(val);
        if (isEmpty(strVal)) return defVal;
        return val;

    }

    /**
     * 获取字符串长度
     **/
    public static int length(CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

    /**
     * 如果是true执行自定义函数
     **/
    public static void isTrue(Boolean cs, FMsSelfBehavior fuc) {
        if (cs) fuc.behavior();
    }

    /**
     * 如果是false执行自定义函数
     **/
    public static void isFalse(Boolean cs, FMsSelfBehavior fuc) {
        if (!cs) fuc.behavior();
    }

    /**
     * 判断集合长度是否为0
     **/
    public static boolean isCollectZero(Collection collection) {
        if (collection == null || collection.size() == 0) return true;
        return false;
    }

    /**
     * 如果集合为0需要进行的行为
     **/
    public static void isCollectZero(Collection collection, FMsSelfBehavior fuc) {
        if (collection == null || collection.size() == 0) fuc.behavior();
    }

    /**
     * 如果集合不为空进行的行为
     **/
    public static void isCollectNotZero(Collection collection, FMsSelfBehavior fuc) {
        if (collection != null && collection.size() != 0) fuc.behavior();
    }


    /**
     * 如果map长度为0或null返回true
     **/
    public static boolean isCollectZero(Map map) {
        if (map == null || map.size() == 0) return true;
        return false;
    }


}
